'use strict';

const Controller = require('egg').Controller;
const Mockdata = require('../mock/ares')

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  //根据获取到的数据渲染到我们的navicat表格中
  async mock() {
    const { ctx, app } = this;
    await Mockdata.map((item) => {
      app.mysql.query(`insert into state(img,name,city,city1,city2,img1,money,title,time,ares,price,company,type,citys,screen,one,word,house,borker,open,phone,views,pay,give,place,site,cityone,senscs,housescreen) values("${item.img}","${item.name}","${item.city}","${item.city1}","${item.city2}","${item.img1}","${item.money}","${item.title}","${item.time}","${item.ares}","${item.price}","${item.company}","${item.type}","${item.citys}","${item.screen}","${item.one}","${item.word}","${item.house}","${item.borker}","${item.open}","${item.phone}","${item.views}","${item.pay}","${item.give}","${item.place}","${item.site}","${item.cityone}","${item.senscs}","${item.housescreen}")`)
    })
    ctx.body = {
      code: 200,
      msg: "成功",
      Mockdata
    }
  }

  //通过表格里获取到数据去渲染
  async list() {
    const { ctx, app } = this;
    let data = await app.mysql.select("state")
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //通过表格里获取到数据去渲染
  async getlist() {
    const { ctx, app } = this;
    let data = await app.mysql.select("state")
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //通过获取id来实现的删除功能
  async del() {
    const { ctx, app } = this;
    let data = await app.mysql.delete("state", { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //获取数据库的数据判断，是否有符合我们需要筛选的数据(团购页面)
  async area() {
    const { ctx, app } = this;
    const { select5 } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { type: select5 } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  //获取数据库的数据判断，是否有符合我们需要筛选的数据(经纪人)
  async borker() {
    const { ctx, app } = this;
    const { seal } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { borker: seal } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  //获取数据库的数据判断，是否有符合我们需要筛选的数据(经纪人)
  async live() {
    const { ctx, app } = this;
    const { select } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { screen: select } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  //获取数据库的数据判断，是否有符合我们需要筛选的数据(经纪人)
  async indent() {
    const { ctx, app } = this;
    const { values } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { house: values } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  //按姓名搜索
  async select() {
    const { ctx, app } = this
    const { value } = ctx.request.body;
    const data = await app.mysql.query(`select * from state where name like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //按标题搜索
  async select1() {
    const { ctx, app } = this
    const { value } = ctx.request.body;
    const data = await app.mysql.query(`select * from state where title like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async getAdd(){
    const {ctx,app}=this;
    const {id,title,word,site,city,name,time,price,}
  }


  async detail() {
    const { ctx, app } = this;
    const data = await app.mysql.get('state', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async getCity() {
    const { ctx, app } = this;

    const { value } = ctx.request.body
    console.log(value.area.value);

    const data = await app.mysql.query(`select * from state where area like "%${value.area.value}%"`)
    console.log(data);

    ctx.body = {
      code: "1",
      msg: '请求成功',
      data
    }
  }



}

module.exports = HomeController;